package cn.mygweb.admin.modules.index;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.mygweb.common.controller.BaseController;
import cn.mygweb.common.pojo.JsonResult;

/** 
* <p>Title: AdminIndexController.java</p>
* <p>Description: 后台主页面</p>  
* @author gxb 
* @date 2019年6月1日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/ 
@Controller
@RequestMapping("/admin")
public class IndexController extends BaseController{
	
	/**
	 * <p>Title: index</p>
	 * <p>Description: 后台主页面</p>
	 * @return
	 */
	@RequestMapping("/index")
	public String index(ModelMap model) {
		//从shiro中获取登录的user信息并返回给前端页面
		userService.setLoginUser(model);

		int webIndexCount = 0;	//前台访问次数
		int adminIndexCount = 0; //获取后台访问次数
		
		//1.统计前台访问数据，传递给页面
		webIndexCount = sysVisitService.selectCountByType(1);
		
		//2.统计后台访问数据，传递给页面
		sysVisitService.AddOneCount(2);
		adminIndexCount = sysVisitService.selectCountByType(2);
		
		Map<String, Object> maps = new HashMap<String, Object>();
		maps.put("webIndexCount", webIndexCount);
		maps.put("adminIndexCount", adminIndexCount);
		model.addAllAttributes(maps);
		
		return "admin/modules/index";
	}
	
	/**
	 * <p>Title: clearCaches</p>
	 * <p>Description: 清除所有redis缓存</p>
	 * @return
	 */
	@RequestMapping("/clearCaches")
	@ResponseBody
	public JsonResult clearCaches() {
		try {
			redisUtils.delAll();
			return JsonResult.ok("清除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.errorMsg("清除失败");
		}		
	}
	
	/**
	 * <p>Title: unauthorized</p>
	 * <p>Description: 没有访问该模块的权限</p>
	 * @return
	 */
	@RequestMapping("/error/unauthorized")
	public String unauthorized() {
		return "admin/error/unauthorized";
	}
}
